package com.keymozy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.keymozy.annotaion.NoAuthentication;
import com.keymozy.pojo.Dept;
import com.keymozy.service.DeptService;
import com.keymozy.state.Code;
import com.keymozy.state.RespData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/dept")
@CrossOrigin
public class DeptController {
    @Autowired
    private DeptService deptService;
    @PostMapping("/getAll/{current}/{size}")
    @NoAuthentication
    public RespData getAll(@PathVariable(value = "current",required = false)Integer current,
                           @PathVariable(value = "size",required = false)Integer size,
                           @RequestBody(required = false) Dept dept){
        if(current==null||size==null){
            current=1;
            size=2;
        }
        return new RespData(Code.SUCCESS_DEFT_1,deptService.depts(current,size,dept));
    }
    @DeleteMapping("/{id}/{current}/{size}")
    public RespData delDept(@PathVariable(value = "id",required = false)Integer id,
                            @PathVariable(value = "size",required = false)Integer size,
                            @PathVariable(value = "current",required = false)Integer current){
        if(size==1){
          current=current-1;
        }
        boolean b = deptService.removeById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("current",current);
        if(b){
            map.put("msg","删除部门成功");
            return new RespData(Code.SUCCESS_DELETE_1,map);
        }else {
            map.put("msg","删除部门失败");
            return new RespData(Code.FALSE_DELETE,map);
        }
    }
    @PutMapping("/{id}")
    public RespData updDept(@RequestBody Dept dept,@PathVariable("id") Integer id){
        boolean update = deptService.update(dept, new LambdaQueryWrapper<Dept>().eq(Dept::getId,id));
        if(update){
            return new RespData(Code.SUCCESS_UPDATE_1,"修改部门信息成功");
        }
        return new RespData(Code.FALSE_UPDATE,"修改部门信息失败");
    }
    @GetMapping
    public RespData echartDepts(){
        return new RespData(Code.SUCCESS_SEARCH,deptService.echartDept());
    }
}
